﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DoAnPhatTrienPhanMem.Models;

namespace DoAnPhatTrienPhanMem.Controllers
{
    public class PhongKhoaHocController : BaseController
    {
        //
        // GET: /PhongKhoaHoc/

        public ActionResult Index()
        {
            var ID = Request["IDvien"];
            ViewData["IDvien"] = ID;
            ViewData["TenVien"] = helper.GetOne<Vien>(s => s.ID.ToString() == ID).TenVien;
            return View();
        }

        public ActionResult Details()
        {
            var ID = Request["ID"];
            ViewData["ID"] = ID;
            ViewData["TenPhong"] = helper.GetOne<Phong>(s => s.ID.ToString() == ID).TenPhong;
            var MaVien = helper.GetOne<Phong>(s => s.ID.ToString() == ID).MaVien;
            ViewData["IDvien"] = helper.GetOne<Vien>(s => s.MaVien == MaVien).ID;
            return View();
        }

        public ActionResult AddOrEdit()
        {
            var IDvien = Request["IDvien"];
            var ID = Request["ID"];
            ViewData["ID"] = "";
            ViewData["MaVien"] = "";
            ViewData["TenVien"] = "";
            ViewData["IDvien"] = IDvien;
            if (!String.IsNullOrEmpty(ID))
            {
                ViewData["ID"] = ID;
                ViewData["MaVien"] = helper.GetOne<Phong>(s => s.ID.ToString() == ID).MaVien;
                ViewData["TenVien"] = helper.GetOne<Vien>(s => s.MaVien.ToString() == ViewData["MaVien"].ToString()).TenVien;
            }
            var vien = helper.GetOne<Vien>(s => s.ID.ToString() == IDvien);
            if (vien != null)
            {
                ViewData["MaVien"] = vien.MaVien;
                ViewData["TenVien"] = vien.TenVien;
            }

            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult Save(FormCollection values)
        {
            var result = new Dictionary<String, Object>();

            try
            {
                var ID = values["ID"];
                var MaVien = values["MaVien"];
                var entity = String.IsNullOrEmpty(ID)
                             ? new Phong()
                             : helper.GetOne<Phong>(s => s.ID.ToString() == ID);
                if (entity == null)
                {
                    return WarningMessage("Không tồn tại");
                }
                // Load data to entity
                entity = (Phong)GetDataFromForm(entity, values);
                if (entity.ID == Guid.Empty)
                {
                    #region Insert
                    if (helper.EntityExists<Phong>(s => s.TenPhong == entity.TenPhong && s.MaVien == entity.MaVien))
                    {
                        return WarningMessage("Tên đã tồn tại");
                    }
                    entity.ID = Guid.NewGuid();
                    helper.Insert(entity);
                    #endregion
                }
                else
                {
                    #region Update
                    if (helper.EntityExists<Phong>(s => s.TenPhong == entity.TenPhong && s.ID.ToString() != ID && s.MaVien == s.MaVien))
                    {
                        return WarningMessage("Tên đã tồn tại");
                    }
                    helper.Update<Phong>(entity, s => s.ID.ToString() == ID);
                    #endregion
                }
                result.Add("success", true);
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            catch (Exception e)
            {
                return WarningMessage("Lỗi :" + e.Message);
            }
        }

        public JsonResult Delete(Guid ID)
        {
            IDictionary<String, Object> result = new Dictionary<String, Object>();
            try
            {
                var SanPham = helper.GetOne<Phong>(s => s.ID == ID);
                if (SanPham == null)
                {
                    return WarningMessage("Phòng không tồn tại");
                }
                helper.Delete<Phong>(s => s.ID == ID);
                result.Add("success", true);
            }
            catch (Exception e)
            {
                return WarningMessage("Lỗi :" + e.Message);
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetByVien()
        {
            var result = new Dictionary<String, Object>();
            try
            {
                var IDvien = Request["IDvien"];

                var MaVien = (!String.IsNullOrEmpty(Request["MaVien"])) ? int.Parse(Request["MaVien"]) : 0;
                if (!String.IsNullOrEmpty(IDvien)) MaVien = helper.GetOne<Vien>(s => s.ID.ToString() == IDvien).MaVien;
                var resultList = helper.GetTable<PhongOverView>().ToList();
                resultList = resultList.Where(s => s.MaVien == MaVien).OrderBy(s => s.TenPhong).ToList();
                var tochuc = helper.GetTable<ToChucCanBo>().ToList();
                foreach (var phong in resultList)
                {
                    phong.SLcanbo = tochuc.Count(s => s.MaPhongBan == phong.MaPhong && s.ThuocBanVien == "VIEN");
                }
                result.Add("data", resultList);
            }
            catch (Exception e)
            {
                return WarningMessage("Lỗi :" + e.Message);
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetByID()
        {
            var ID = Request["ID"];
            var result = new Dictionary<String, Object>();
            var resultList = helper.GetTable<Phong>().ToList();
            resultList = resultList.Where(s => s.ID.ToString() == ID).ToList();
            result.Add("data", resultList);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetDetailsByID()
        {
            var ID = Request["ID"];
            var result = new Dictionary<String, Object>();
            var resultList = helper.GetTable<PhongDetailsView>().Where(s => s.ID.ToString() == ID).ToList();
            //var ban = helper.GetOne<BanHanhChinhDetailsView>(s => s.ID.ToString() == ID);
            var tochuc = helper.GetTable<ToChucCanBoChucDanhView>().ToList();
            foreach (var phong in resultList)
            {
                tochuc = tochuc.Where(s => s.MaPhongBan == phong.MaPhong && s.ThuocBanVien == "VIEN").ToList();
                phong.SLcanbo = tochuc.Count();
                phong.GS = tochuc.Count(s => s.HocHam == Constants.Constants.ChucDanh.HocHam.GIAOSU);
                phong.PGS = tochuc.Count(s => s.HocHam == Constants.Constants.ChucDanh.HocHam.PHOGIAOSU);
                phong.TSKH = tochuc.Count(s => s.HocVi == Constants.Constants.ChucDanh.HocVi.TIENSIKHOAHOC);
                phong.TS = tochuc.Count(s => s.HocVi == Constants.Constants.ChucDanh.HocVi.TIENSI);
                phong.PTS = tochuc.Count(s => s.HocVi == Constants.Constants.ChucDanh.HocVi.PHOTIENSI);
                phong.THS = tochuc.Count(s => s.HocVi == Constants.Constants.ChucDanh.HocVi.THACSI);
                phong.Truong = "";
                phong.Pho = "";
                var truong = helper.GetOne<ToChucCanBo>(s => s.ThuocBanVien == "VIEN" && s.MaPhongBan == phong.MaPhong && s.ViTri == Constants.Constants.ViTri.TRUONG);
                if (truong != null)
                {
                    var canbo = helper.GetOne<SoYeuCanBo>(s => s.MaCB == truong.MaCB);
                    phong.Truong = canbo.HocHam + " " + canbo.HocVi + " " + canbo.HoTen;
                }
                var pho = helper.GetOne<ToChucCanBo>(s => s.ThuocBanVien == "VIEN" && s.MaPhongBan == phong.MaPhong && s.ViTri == Constants.Constants.ViTri.PHO);
                if (pho != null)
                {
                    var canbo = helper.GetOne<SoYeuCanBo>(s => s.MaCB == pho.MaCB);
                    phong.Pho = canbo.HocHam + " " + canbo.HocVi + " " + canbo.HoTen;
                }
            }
            result.Add("data", resultList);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public Phong GetPhongByID(String ID)
        {
            var data = new Phong();
            try
            {
                data = helper.GetOne<Phong>(s => s.ID.ToString() == ID);
            }
            catch
            {
            }
            return data;
        }
    }
}
